import { http } from "@/utils/http";

/** 设备AST日志模型定义 */
export type DeviceAstLogModel = {
  id: number;
  deviceNo: string;
  deptId: number;
  createdAt: string;
  data: number;
  detail: string;
  sampletime: number;
};

/** 设备模型定义 */
export type DeviceModel = {
  id: number;
  deviceNo: string;
  name: string;
  channelNo: string;
  productId: number;
  productName: string;
  deptId: number;
  deptName: string;
  groupId: number | null;
  groupName: string;
  type: number;
  status: number;
  statusTime: string;
  version: string;
  createdAt: string;
  image: string;
  openStatus: number;
  mqttServer: string;
  databaseServer: string;
  ossServer: string;
  parentId: number | null;
  networkType: number | null; //'入网方式1-4g，2-lora',
};

/** 分页获取设备列表 */
export const getDeviceListApi = (data: IReqPagerData) => {
  return http.request<Resp<IRespPagerData<DeviceModel>>>(
    "get",
    "/device/page-list",
    { params: data }
  );
};

/** 新增设备 */
export const addDeviceApi = (data: Partial<DeviceModel>) => {
  return http.request<Resp<null>>("post", "/device", { data });
};

/** 更新设备 */
export const updateDeviceApi = (id: number, data: Partial<DeviceModel>) => {
  return http.request<Resp<null>>("put", `/device/${id}`, { data });
};

/** 删除设备 */
export const deleteDeviceApi = (id: number) => {
  return http.request<Resp<null>>("delete", `/device/${id}`);
};

/** 根据ID获取设备详情 */
export const getDeviceByIdApi = (id: number) => {
  return http.request<Resp<{
    info: DeviceModel
  }>>('get', '/device/getOne', {
    params: { id }
  });
};

/** 获取设备选项列表 */
export const getDeviceOptionsApi = (params?: { fieldKey?: string; fieldLabel?: string ; deptId?:number;productId?:number;deviceGroupId?:number;type?:string }) => {
  return http.request<Resp<{
    options: Array<{
      label: string;
      val: string | number;
    }>
  }>>('get', '/device/getOptions', { params });
};

/** sendcmd 发送命令 */
export const sendcmdApi = (data: any) => {
  return http.request<Resp<null>>("post", "/device/sendcmd", { data });
};

/** getDeviceStateLogApi 获取设备状态日志 */
export const getDeviceStateLogApi = (params: any) => {
  return http.request<Resp<null>>("get", "/device/getStateLogs", { params });
};

/** getDeviceAstLogsApi 获取设备AST日志 */
export const getDeviceAstLogsApi = (params: { deviceNo: string }) => {
  return http.request<Resp<{
    list: DeviceAstLogModel[]
  }>>("get", "/device/getAstLogs", { params });
};

/** getDeviceCopiesApi 获取可复制设备列表 */
export const getDeviceCopiesApi = (params: { deviceNo: string }) => {
  return http.request<Resp<{
    list: DeviceModel[]
  }>>("get", "/device/getCopies", { params });
};
